/*
 * @# PersonController.java 2011-12-29 上午10:26:36
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.person;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ianswer.px.posts.entity.PostData;
import com.ianswer.px.posts.service.PostService;
import com.ianswer.px.user.entity.UserBaseInfo;
import com.ianswer.px.user.service.FollowService;

/**
 * @author pananz
 */
@Controller
public class PersonController {

	private static final int max = 20;
	private static final int homeSkip = 0;

	@Resource(name = "followService")
	private FollowService followService;

	@Resource(name = "postService")
	private PostService postService;

	/**
	 * 个人主页
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/u/{userId}/home", method = RequestMethod.GET)
	public String home(@PathVariable String userId, Model model) {
		//个人, 滚动框，秀场，名品，博文，圈友
		try {
			List<UserBaseInfo> follows = followService.queryFollow(Long.parseLong(userId), homeSkip, max);
			List<PostData> posts = postService.queryPosts(Long.parseLong(userId), homeSkip, max);
			UserBaseInfo user = UserBaseInfo.get(Long.parseLong(userId));
			user.setId(Long.parseLong(userId));
			model.addAttribute("follows", follows);
			model.addAttribute("posts", posts);
			model.addAttribute("user", user);
			return "person/home";
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/error";
		}
	}

	/**
	 * 秀场
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/u/{userId}/show", method = RequestMethod.GET)
	public String show(@PathVariable String userId, Model model) {
		UserBaseInfo user = UserBaseInfo.get(Long.parseLong(userId));
		user.setId(Long.parseLong(userId));
		model.addAttribute("user", user);
		return "person/show";
	}

	/**
	 * 名品展示
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/u/{userId}/famous", method = RequestMethod.GET)
	public String famous(@PathVariable String userId, Model model) {
		UserBaseInfo user = UserBaseInfo.get(Long.parseLong(userId));
		user.setId(Long.parseLong(userId));
		model.addAttribute("user", user);
		return "person/show";
	}

	/**
	 * 微博
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/u/{userId}/post", method = RequestMethod.GET)
	public String post(@PathVariable String userId, HttpServletRequest request, Model model) {
		int skip = 0;
		String rp = request.getParameter("rp");
		if (StringUtils.isNotBlank(rp)) {
			skip = Integer.parseInt(rp);
		}
		List<PostData> posts = postService.queryPosts(Long.parseLong(userId), skip, max);
		UserBaseInfo user = UserBaseInfo.get(Long.parseLong(userId));
		user.setId(Long.parseLong(userId));
		model.addAttribute("posts", posts);
		model.addAttribute("user", user);
		return "person/post";
	}

	/**
	 * 人脉
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/u/{userId}/contacts", method = RequestMethod.GET)
	public String contacts(@PathVariable String userId, Model model) {

		UserBaseInfo user = UserBaseInfo.get(Long.parseLong(userId));
		user.setId(Long.parseLong(userId));
		model.addAttribute("user", user);
		return "person/contacts";
	}

	/**
	 * 个人档案
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/u/{userId}/info", method = RequestMethod.GET)
	public String info(@PathVariable String userId, Model model) {
		UserBaseInfo user = UserBaseInfo.get(Long.parseLong(userId));
		user.setId(Long.parseLong(userId));
		model.addAttribute("user", user);
		return "person/info";
	}

	/**
	 * 她的粉丝
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/u/{userId}/fans", method = RequestMethod.GET)
	public String fans(@PathVariable String userId, Model model) {
		try {
			int skip = 0;
			List<UserBaseInfo> follows = followService.queryFans(Long.parseLong(userId), skip, max);
			UserBaseInfo user = UserBaseInfo.get(Long.parseLong(userId));
			user.setId(Long.parseLong(userId));
			model.addAttribute("user", user);
			model.addAttribute("follows", follows);
			model.addAttribute("leftType", "fans");
			return "person/contacts";
		} catch (Exception e) {
			return "redirect: /error";
		}
	}

	/**
	 * 她的关注
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/u/{userId}/follow", method = RequestMethod.GET)
	public String follow(@PathVariable String userId, Model model) {
		try {
			int skip = 0;
			List<UserBaseInfo> follows = followService.queryFollow(Long.parseLong(userId), skip, max);
			UserBaseInfo user = UserBaseInfo.get(Long.parseLong(userId));
			user.setId(Long.parseLong(userId));
			model.addAttribute("user", user);
			model.addAttribute("follows", follows);
			model.addAttribute("leftType", "follow");
			return "person/contacts";
		} catch (Exception e) {
			return "redirect: /error";
		}
	}

	@RequestMapping(value = "error", method = RequestMethod.GET)
	public String error(Model model) {
		return "error";
	}
}
